package com.wwwarehouse.contract.contract_address;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.contract.adapter.ChooseAddressAdapter;
import com.wwwarehouse.contract.bean.AddressListBean;
import com.wwwarehouse.contract.contract.TheParentFragment;
import com.wwwarehouse.contract.core.ContractConstant;
import com.wwwarehouse.contract.event.RefreshShoppCardEvent;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import contract.wwwarehouse.com.contract.R;
import de.greenrobot.event.EventBus;

/**
 * Created by feng.gao on 2017/10/19.
 * 选择地址列表 listview
 */

public class ManagerAddressListViewFragment extends TheParentFragment {
	private ListView mListView;
	private List<AddressListBean.ListBean> list = new ArrayList<>();
	private ChooseAddressAdapter chooseAddressAdapter;
	private int mHeight;
	private String demanId;
	private PopupWindow popupWindow;

	@Override
	protected int getContentId() {
		return R.layout.choose_address_listview_layout;
	}

	@Override
	protected void init(View view) {
		mListView = (ListView) view.findViewById(R.id.choose_lv);
		mListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
				if (list.get(position).getIsDefault().equals("1")) {
					//已经是默认的话 不显示默认
					shwoPopupWindow(view, position, false);
				} else {
					shwoPopupWindow(view, position, true);
				}
				return true;
			}
		});
	}

	@Override
	public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
		if (enter) {
			if (getArguments() != null && getArguments().getSerializable(ContractConstant.KEY_BUNDLE_VIEW_PAGER_DATA) != null) {
				final AddressListBean listBean = (AddressListBean) getArguments().getSerializable(ContractConstant.KEY_BUNDLE_VIEW_PAGER_DATA);
				demanId = listBean.getDemanId();
				int position = getArguments().getInt(ContractConstant.KEY_BUNDLE_POSITION);
				int pageNum = getArguments().getInt(ContractConstant.KEY_BUNDLE_PAGENUM);
				list.clear();
				for (int i = position * pageNum; i < (position + 1) * pageNum; i++) {
					if (i >= 0 && i < listBean.getList().size()) {
						list.add(listBean.getList().get(i));
					}
				}
				mListView.post(new Runnable() {
					@Override
					public void run() {
						chooseAddressAdapter = new ChooseAddressAdapter(list, mActivity , mListView.getHeight() - mListView.getPaddingBottom() - 20);
						chooseAddressAdapter.setIsManager(true);
						chooseAddressAdapter.setOnMoreClickListner(new ChooseAddressAdapter.onMoreClickListener() {
							@Override
							public void onMore(View view, int position) {
								if (list.get(position).getIsDefault().equals("1")) {
									//已经是默认的话 不显示默认
									shwoPopupWindow(view, position, false);
								} else {
									shwoPopupWindow(view, position, true);
								}
							}
						});
						mListView.setAdapter(chooseAddressAdapter);
					}
				});

//
			}
		}
		return null;
	}


	@Override
	public void setTitle() {

	}

	@Override
	public void requestDatas() {




	}

	@Override
	public void onSuccess(CommonClass commonClass, int what) {
		if (what == 1 || what == 2) {
			if (commonClass.getCode().equals("0")) {
				//刷新vewpager
				EventBus.getDefault().post(new RefreshShoppCardEvent("refresh" , 0));
			}
		}

	}

	@Override
	public void onReLoad(int code) {

	}

	private void shwoPopupWindow(View view, final int position , boolean isShowDefaule) {
		//用来存放当前的item的坐标值，第一个是x的坐标，第二个是y的坐标
		int[] location = new int[2];
		//把当前的item的坐标放到int数组里面
		view.getLocationInWindow(location);
		View popupView = View.inflate(mActivity, R.layout.address_item_popupwindow_layout, null);
		//new 一个PopupWindow出来
		popupWindow = new PopupWindow(popupView, view.getWidth(), view.getHeight() - 20);
		//一定要给popupWindow设置一个背景图片，不然的话，会有很多未知的问题的
		//如没办法给它加上动画，还有显示会有问题等。
		//如果我们没有要设置的图片，那么我们就给它加上一个透明的背景图片
		Drawable drawable = new ColorDrawable(Color.TRANSPARENT);
		FrameLayout mChangeTimeLayout = (FrameLayout) popupView.findViewById(R.id.change_time_layout);
		FrameLayout mChangePayLayout = (FrameLayout) popupView.findViewById(R.id.change_pay_layout);
		FrameLayout mDeleteLayout = (FrameLayout) popupView.findViewById(R.id.delete_layout);
		if (isShowDefaule) {
			mChangeTimeLayout.setVisibility(View.VISIBLE);
		} else {
			mChangeTimeLayout.setVisibility(View.GONE);
		}
		mChangeTimeLayout.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				//设为默认
				Map<String, Object> map = new HashMap<>();
				map.put("addressCommonUkid", list.get(position).getAddressCommonUkid());
				map.put("isDefault", "1");
				map.put("type", "default");
				map.put("demandId", demanId);
				httpPost(ContractConstant.ADDRESS_EDIT_METHOD , map , 1 , true , "加载中...");
				popupWindow.dismiss();
			}
		});
		mChangePayLayout.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				//编辑
				Bundle bundle = new Bundle();
				bundle.putSerializable(ContractConstant.KEY_EDIT_ADDRESS_LIST_DAO , list.get(position));
				bundle.putString(ContractConstant.KEY_CONSTRACT_PLACE_ORDER_DEMANID , demanId);
				bundle.putString("isDefault" , list.get(position).getIsDefault());
				UpdateAddressFragment updateAddressFragment = new UpdateAddressFragment();
				updateAddressFragment.setArguments(bundle);
				pushFragment(updateAddressFragment);
				popupWindow.dismiss();

			}
		});
		mDeleteLayout.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				//移除
				Map<String, Object> map = new HashMap<>();
				map.put("addressCommonUkid", list.get(position).getAddressCommonUkid());
				map.put("type", "delete");
				map.put("demandId", demanId);
				httpPost(ContractConstant.ADDRESS_EDIT_METHOD , map , 2 , true , "加载中...");
				popupWindow.dismiss();

			}
		});
		popupWindow.setBackgroundDrawable(drawable);
		popupWindow.setOutsideTouchable(true);
		int x = location[0];
		int y = location[1];
		//把popupWindow显示出来
		popupWindow.showAtLocation(view, Gravity.LEFT | Gravity.TOP, x, y);
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		if (popupWindow != null && popupWindow.isShowing()) {
			popupWindow.dismiss();
		}
	}
}
